/**
* @Title: WebInitializer.java
* @Package com.an.SpringMvcl
* @Description: TODO(用一句话描述该文件做什么)
* @author antch,861710865@qq.com
* @date 2017年7月14日 上午10:56:00
* @version V1.0
*/

package com.an.SpringMvcl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.util.Log4jConfigListener;

/**
 * @ClassName: WebInitializer
 * @Description: TODO(Web配置)
 * @author antch,861710865@qq.com
 * @date 2017年7月14日 上午10:56:00
 *
 */
/*
 * WebApplicationInitializer此接口是spring提供用来配置servlet3.0+配置的接口，
 * 从而实现了替代web.xml的位置。实现此接口将会自动被SpringServletContainerInitializer获取到
 */
@SuppressWarnings("deprecation")
public class WebInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("***************");
		System.out.println("****加载类似web.xml类*****");
		System.out.println("***************");

		// 日志配置
		servletContext.setInitParameter("log4jConfigLocation", "classpath:log4j.properties");
		servletContext.addListener(Log4jConfigListener.class);


		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(SpringMvcConfig.class);
		context.setServletContext(servletContext);
		Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
		// servletContext.
		servlet.addMapping("/");
		servlet.setLoadOnStartup(1);
		servlet.setAsyncSupported(true);

	}

}
